package com.jckl.foodathon.fragments;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;

import com.jckl.foodathon.R;
import com.jckl.foodathon.common.utils.DateUtil;
import com.jckl.foodathon.model.Blog;
import com.jckl.foodathon.model.Blogger;
import com.jckl.foodathon.model.Store;
import com.jckl.foodathon.services.config.NewBlogConfiguration;
import com.jckl.foodathon.sqlite.dao.DaoSession;
import com.jckl.foodathon.sqlite.model.TB_Blogger;
import com.jckl.foodathon.sqlite.model.TB_Store;
import com.jckl.foodathon.sqlite.services.BloggerRepository;
import com.jckl.foodathon.sqlite.services.SqliteCommon;
import com.jckl.foodathon.sqlite.services.StoreRepository;

public class BlogFragment extends Fragment implements OnClickListener{
	
	//Screen input
	Spinner blogger;
	Spinner store;
	
	EditText title;
	EditText blogURL;
	EditText bloggerName;
	EditText postDate;

	Button save;
	Blog blog;
	
	public BlogFragment () {
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.fragment_blog, container, false);
		
		save		= (Button) view.findViewById(R.id.btn_blog_save);
		save.setOnClickListener(this);
		
		blogger			= (Spinner) view.findViewById(R.id.blog_blogger);
		store			= (Spinner)	view.findViewById(R.id.blog_store);
		title			= (EditText) view.findViewById(R.id.blog_title);
		blogURL 		= (EditText) view.findViewById(R.id.blog_url);
		postDate		= (EditText) view.findViewById(R.id.blog_postdate);
		
		// Elements from sqlite
		SqliteCommon sqliteCommon = new SqliteCommon();
		DaoSession session = sqliteCommon.setupDatabase(getActivity().getApplicationContext());
		List<TB_Blogger> sqlite_bloggers = BloggerRepository.getAllBloggers(session);
		List<TB_Store> sqlite_stores = StoreRepository.getAllStores(session);
		
		// Spinner blogger elements
		List<String> bloggers = new ArrayList<String>();
		bloggers.add(getResources().getString(R.string.none));
		for (TB_Blogger tb_blogger : sqlite_bloggers) {
			bloggers.add(tb_blogger.getName());
		}
		ArrayAdapter<String> bloggerAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, bloggers);
		bloggerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		blogger.setAdapter(bloggerAdapter);
		
		//Spinner store elements
		List<String> stores = new ArrayList<String>();
		stores.add(getResources().getString(R.string.none));
		for (TB_Store tb_store : sqlite_stores) {
			stores.add(tb_store.getName());
		}
		ArrayAdapter<String> storeAdapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, stores);
		storeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		store.setAdapter(storeAdapter);
		
		return view;
	}

	@Override
	public void onClick(View v) {
		switch(v.getId()){
		case R.id.btn_blog_save:
			saveStoreData();
			break;
		}
	}
	
	private void setDataToObject(){
		blog = new Blog();
		Blogger blogger = new Blogger();
		blogger.setName(this.blogger.getSelectedItem().toString());
		
		Store store = new Store();
		store.setName(this.store.getSelectedItem().toString());
		
		blog.setBlogger(blogger);
		blog.setStore(store);
		blog.setBlogURL(this.blogURL.getText().toString());
		blog.setTitle(this.title.getText().toString());
		blog.setPostDate(DateUtil.getDate(this.postDate.getText().toString(), "dd-MM-yyyy"));
	}
	
	private void saveStoreData(){
		setDataToObject();
		NewBlogConfiguration blogConfig = new NewBlogConfiguration();
		blogConfig.perform(getActivity(), blog);
	}
}
